
var CONFIG = {
	GET_AREA_DEFAULT_ID : 12, //城市搜索控件默认选中福建省
	provinces : null
};
var G = {
	is : (function(){
		var Sys = {};
		var ua = navigator.userAgent.toLowerCase();
		var s;
		(s = ua.match(/rv:([\d.]+)\) like gecko/)) ? Sys.ie = s[1] :
		(s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
		(s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
		(s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
		(s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
		(s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
		return Sys
	})()
}
var S = (function(){
	var CacheAreas = {};
	var seah_inp_text = "景区产品名称 / 供应商";
	var urls = {
		p : "call/getAreas.php",
		c : "call/getAreas.php"
	}
	function getProvince(callback){
		$.ajax({
			url : urls.p,
			type : "GET",
			dataType : "json",
			data : {
				act : "getProvince"
			},
			success : function(res){
				CONFIG.provinces = res; //数组
				callback && typeof callback == "function" && callback(res);
			},
			error : function(xhr,textStatus){
				callback && typeof callback == "function" && callback(textStatus,xhr)
			}
		})
	}
	
	function getCity(parentId,callback){
		if(typeof parentId == "undefined") return;
		$.ajax({
			url : urls.c,
			type : "GET",
			dataType : "json",
			data : {
				act : "getCity",
				parentId : parentId,
				area_deeppath : 2,
				area_id : 381
			},
			success : function(res){
				callback && typeof callback == "function" && callback(res);
			},
			error : function(xhr,textStatus){
				callback && typeof callback == "function" && callback(textStatus,xhr)
			}
		})
	}

	function init(callback){
		var pselect = $("#provSelect");
		var cselect = $("#citySelect");
		var dselect = $("#conSelect");
		var serhInp = $("#serhInp");
		var s = G.is.ie;
		serhInp.val(seah_inp_text).on("focus",function(e){
			var t = $(e.target);
			t.addClass("onfocus");
			if(!s || parseFloat(s)>8){
				if(t.val() == seah_inp_text){
					t.val("");
				}
			}
		}).on("blur",function(e){
			var t = $(e.target);
			t.removeClass("onfocus");
			if(!s || parseFloat(s)>8){
				if(t.val() == ""){
					t.val(seah_inp_text);
				}
			}
		}).on("input propertychange",function(e){
			filter($(e.target))
		})
		//ie6 7 8不支持input事件 用onpropertychange
		/*if(s && parseFloat(s)<9){
			document.getElementById('serhInp').attachEvent('onpropertychange',function(e) {
				if(e.propertyName.toLowerCase()!='value') return;
				filter($(e.srcElement));
			});
		} */
		$("#provSelect").change(function(e){
			List.loading();
			var val = $(e.target).val();
			var data;
			if(val == "00"){
				data = List.getData(1);
				List.buildData(data);
				cselect.empty();
				cselect.append('<option value="00">全部</option>');
				return;
			}else{
				data = List.getData("search",function(odata){
					var data = {};
					for(var i in odata){
						var provid = odata[i]["areas"][0];
						if(val == provid){
							data[i] = odata[i];
						}
					}
					return data;
				})
				List.buildData(data,"search");
			}
			if(CacheAreas[val] && CacheAreas[val]["citys"]){
				cselect.empty();
				cselect.append('<option value="00">全部市(区)</option>')
				for(var cid in CacheAreas[val]["citys"]){
					var c = CacheAreas[val]["citys"][cid];
					select.append('<option value="'+cid+'">'+c.area_name+'</option>')
				}
			}else{
				getCity(val,function(cres,xtext){
					if(typeof cres !== "string" && cres.length){
						cselect.empty();
						cselect.append('<option value="00">全部市(区)</option>');
						CacheAreas[val]["city"] = {};
						$.each(cres,function(index,data){
							CacheAreas[val]["city"][data.area_id] = data.area_name;
							cselect.append('<option value="'+data.area_id+'">'+data.area_name+'</option>')
						})
					}else{
						alert("获取市级列表失败！")
					}
				})
			}
			
		})
		$("#citySelect").change(function(e){
			List.loading();
			var val = $(e.target).val();
			var data = List.getData("search",function(odata){
				var data = {};
				for(var i in odata){
					var cityid = odata[i]["areas"][1];
					if(val == cityid){
						data[i] = odata[i];
					}
				}
				return data;
			})
			List.buildData(data,"search");
		})
		getProvince(function(res,xtext){
			//console.log(res)
			if(typeof res !== "string" && res.length){
				pselect.append('<option value="00">全部省份</option>');
				$.each(res,function(index,data){
					CacheAreas[data.area_id] = {};
					CacheAreas[data.area_id]["name"] = data.area_name;
					pselect.append('<option value="'+data.area_id+'">'+data.area_name+'</option>');
					//设置福建省为默认省份
					//pselect.find("option[value='"+CONFIG.GET_AREA_DEFAULT_ID+"']").attr("selected",true);
				})
				callback && typeof callback == "function" && callback();
				getCity(CONFIG.GET_AREA_DEFAULT_ID,function(cres,xtext){
					if(typeof cres !== "string" && cres.length){
						cselect.append('<option value="000">全部市级</option>');
						$.each(cres,function(index,data){
							cselect.append('<option value="'+data.area_id+'">'+data.area_name+'</option>')
						})
					}else{
						alert("获取市级列表失败！")
					}
				})
			}else{
				alert("获取省份列表失败！")
			}
		})
	}
	
	function filter(target){
		var t = target;
		var val = t.val();
		var data = {};
		var cselect = $("#citySelect");
		var pselect = $("#provSelect");
		if(!val){
			data = List.getData(1);
			List.buildData(data);
			cselect.empty();
			cselect.append('<option value="00">全部</option>');
			pselect.find("option[value=00]").attr("selected",true);
			$("#conSelect").find("option[value=1]").attr("selected",true);
			return;
		}
		var cv = $("#conSelect").val();
		List.loading();
		data = List.getData("search",function(odata){
			var data = {};
			var prodTitle = "";
			var gys = "";
			for(var i in odata){
				if(cv == "1"){ //搜景区
					prodTitle = odata[i]["title"];
					if(prodTitle.indexOf(val) == 0){
						data[i] = odata[i]
					}
				}else{ //搜供应商
					gys = odata[i]["apply_dname"];
					if(gys.indexOf(val) == 0){
						data[i] = odata[i]
					}
				}
			}
			return data;
		})
		List.buildData(data,"search");
	}
	
	
	return {
		init : init
	}
	
})();

var List = (function(){
	var AJAX_URL = "prod_sale_dev.html";
	var oData = {};
	var oDataSize = 0;  //数据总数
	var currentPagesize = 1; //当前页数
	var totalpagesize = 0; //总页数
	var psize = 5; //默认分页每页15条
	var pageSwitchTimeout = 0.5 * 1000;
	var loading_html = '<li class="loading" style="text-align:center; line-height:1.5; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">加载数据...</p><img src="images/other/loading_1.gif" alt="" /></li>';
	var saveBtn_otext = $("#saveToNext").text();
	function init(){
		getAll(function(){
			var data = getData(1);
			buildData(data);
			$("#pagenavW").on("click",".nextBtn",function(e){
				if(currentPagesize == totalpagesize){
					alert("当前已是最后一页");
					return false;
				}
				loading();
				setTimeout(nextpage,pageSwitchTimeout);
				return false;
			})
			$("#pagenavW").on("click",".prevBtn",function(e){
				if(currentPagesize == 1){
					alert("当前已是第1页");
					return false;
				}
				loading();
				setTimeout(prevpage,pageSwitchTimeout);
				return false;
			})
			$("#pagenavW").on("click",".cerPageBtn",whichpage);
			$("#pagenavW").on("keyup",".pageInp",function(e){
				var target = $(e.target);
				var oval = parseFloat(target.val()) || 0;
				if(e.keyCode == 13){
					whichpage()
				}else if(e.keyCode == 38){//向上 +
					if(oval == totalpagesize) return
					target.val(oval+1);
				}else if(e.keyCode == 40){ //向下
					if(oval == 1) return
					target.val(oval-1);
				}
			});
			$("#pagenavW").on("focus",".pageInp",function(e){
				var inp = $(e.target);
				var btn = $("#cerPageBtn");
				inp.addClass("onfocus");
				btn.animate({"width":46});
			});
			$("#pagenavW").on("blur",".pageInp",function(e){
				var inp = $(e.target);
				var btn = $("#cerPageBtn");
				inp.removeClass("onfocus");
				btn.animate({"width":0});
			});
			$("#saveToNext").on("click",function(){
				$this = $(this);
				if($this.hasClass("disable")) return false;
				var oData = Count.getSelected();
				var dataArr = [];
				var prodCount = 0;
				for(var i in oData){
					prodCount++;
					var tickets = oData[i]["tickets"];
					for(var s in tickets){
						var json = {};
						json["lid"] = i;
						json["pid"] = s;
						json["aid"] = tickets[s]["aid"];
						json["num"] = tickets[s]["count"];
						dataArr.push(json);
					}
				}
				//if(prodCount<=1){
				//	alert("需两个或两个以上不同景区产品方可打包成套票");
				//	$this.text("提交保存进入下一页").removeClass("disable");
				//	return;
				//}
				$this.text("保存中...").addClass("disable");
				var d = _JSONstringify(dataArr);
				post({"package_data":d});
			})
			$("#titUl .tip").hover(function(){
				var of = $(this).offset();
				var set_left = -50;
				var set_top = 16;
				$("#tipW").show().css({"top":of.top+set_top,"left":of.left+set_left})
			},function(){
				$("#tipW").hide();
			})
			$("#tipW").mouseover(function(){$(this).show();})
			$("#tipW").mouseleave(function(){$(this).hide();})
		})
		
		
	}
	function getAll(callback){
		loading();
		$.ajax({
			url : AJAX_URL,
			type : "GET",
			dataType : "JSON",
			timeout : 15*1000,
			data : { "sync" : "1" },
			success : function(res){
				if(res && res.total>0){
					oData = res.lists;
					oDataSize = res.total
					totalpagesize = Math.ceil(res.total/psize); //计算总页数据有小数就整数部分加1
					callback && typeof callback == "function" && callback();
				}else{
					$("#mlistUl").html('<li style="height:200px; line-height:200px; text-align:center; font-size:12px; color:red;">无数据...</style>');
				}
			},
			error : function(xhr,textStatus){
				if(textStatus && textStatus == "timeout"){
					if(xhr && xhr.abort && typeof xhr.abort == "function"){
						xhr.abort();
						alert("请求产品列表超时，请稍后重试。");
						$("#mlistUl").html('<li style="height:200px; line-height:200px; text-align:center; font-size:12px; color:red;">请求产品列表超时，请稍后重试。</style>');
					}
				}
			}
		})
	}
	function post(data){
		var result = data.package_data;
		var form = $("#resultForm");
		$("#formResult").val(result);
		//console.log(result)
		//return false;
		form.submit();
		
	}
	function prevpage(){ //上一页
		if(currentPagesize == 1) return; 
		$("#pagenavW").find(".pageInp").val("");
		currentPagesize--;
		var data = getData(currentPagesize);
		buildData(data);
	}
	function nextpage(){ //下一页
		if(currentPagesize == totalpagesize) return; 
		$("#pagenavW").find(".pageInp").val("");
		currentPagesize++;
		var data = getData(currentPagesize);
		buildData(data);
	}
	function whichpage(){
		var pageInp = $("#pagenavW").find(".pageInp");
		var val = pageInp.val();
		if(val<=0){
			alert("页数必须大于1");
			pageInp.val("");
		}else if(val>totalpagesize){
			alert("页数不能大于总页数");
			pageInp.val("");
		}else{
			loading();
			setTimeout(function(){
				currentPagesize = val;
				var data = getData(val);
				buildData(data);
			},pageSwitchTimeout);
		}
	}
	function loading(){
		$("#mlistUl").html(loading_html);
	}
	function getData(pageindex,filter){
		var begin = 0;
		var end = 0;
		if(pageindex == "search"){
			begin = 1;
			end = oDataSize;
		}else{
			begin = (pageindex-1) * psize + 1;
			end = pageindex * psize;
		}
		var odata = _getData(begin,end);
		var _data = {};
		if(filter && typeof filter == "function"){
			_data = filter(odata);
			return _data;
		}
		return odata;
	}
	function buildData(data,ifNav){
		var str = "";
		var area = null;
		var is_data_empty = true;
		var selectedData = Count.getSelected();
		//console.log(selectedData)
		for(var i in data){
			is_data_empty = false;
			// var cls = "selected";
			// var lid = data[i]["lid"];
			// if(selectedData[lid]){
				// cls = "selected";
			// }
			//area = _getArea(i);
			str += '<div class="list">';
			str += '<div class="list_top position_a ">';
			str +=     '<div class="title" data="'+data[i]["title"]+'">'+data[i]["title"]+'</div>';
			// str +=     '<div class="level">	';		
			// str += 	   		'<span>推荐级别：<span>';
			// str += 	   		'<div class="select">';
			// str += 				'<span class="select_txt" data="'+data[i]["lid"]+'"></span>';
			// str +=              '<div class="downward"><em></em></div>';
			// str += 				'<ul class="select_list">';
			// str += 					'<li>1</li>'; 
			// str += 					'<li>2</li>';
			// str += 					'<li class="current">3</li>';
			// str += 				'</ul>';
			// str += 			'</div>';
			// str += 		'</div>';
			str += 			'<div class="top_right_a">';
			str +=				'<span class="supplier">供应商</span>';
			str +=				'<span class="supply_price">供货价</span>';
			str +=				'<span class="retail_price">零售价</span>';
			str +=				'<span class="operate">操作</span>';
			str +=			'</div>';
			str +=       '</div>';
			for(var s in data[i]["ticket"]){
				//var lidd = data[i]["ticket"];
				//console.log(s);
				var t = data[i]["ticket"][s];
				var lists =data[i]["ticket"][s]["lists"];
				//console.log(lists);
				var tt = data[i]["ticket"][s]["ttitle"];
				var tid = t["pid"];
				str +=     '<div class="list_con">';
				str +=     '<div class="ticket"><span class="title">'+tt+'</span>ID:'+s+'</div>';
				
				for(var j in lists){
					var dname=lists[j]["dname"];
					var js=lists[j]["js"];
					var ls=lists[j]["ls"];
					var id=lists[j]["id"];
					var active=lists[j]["active"];
					//console.log(jj);
					//var ttt = data[i]["lists"][j];
					str +=     '<ul class="list_data">';
					str +=     '<li><span class="supplier">'+dname+'</span><span class="supply_price">'+js+'</span><span class="retail_price">'+ls+'</span><span class="operate_a color_a">分销价格</span><span class="operate_b"><span class="yujbtn" id="yuj" data-active="'+active+'" data-id="'+id+'"></span></li></ul>';
					
				}
				
				str +='</div>';
			}
			str += '</div>';
		}
		$("#mlistUl").html(str);
		 $(".yujbtn").each(function(){
			var active = $(this).attr("data-active");
			if(active=="1"){
				$(this).addClass("onyuj");
			}
		});

		setTimeout(function(){
			$("#mlistUl").children(":first").addClass("selected").find(".botW").slideDown();
		},200)
		if(oDataSize > psize && typeof ifNav == "undefined"){
			var w = $("#pagenavW");
			w.find(".curentpage").text(currentPagesize);
			w.find(".totalpage").text(totalpagesize);
			w.find(".totalItemSize .n").text(oDataSize);
			$("#pagenavW").show();
		}else{
			$("#pagenavW").hide();
		}
		if(is_data_empty){
			$("#mlistUl").html('<li style="height:200px; line-height:200px; text-align:center; font-size:12px; color:red;">无数据...</style>');
		}
	}
	function _getData(begin,end){ //在oData里从第几条取到第几条
		var data = {};
		var index = 0;
		if(begin-end>=0) return;
		//console.log("begin="+begin+" end="+end)
		for(var i in oData){
			index++;
			if(index>=begin && index<=end){
				data[i] = oData[i];
			}
		}
		return data;
	}
	// function _getArea(prodid){
		// var data = oData[prodid];
		// var area = { province : "", city : ""};
		// if(!data) return area;
		// //area.city = data.ticket[0]["address"];
		// var poid = data.areas[0];
		// //console.log("CONFIG.provinces="+CONFIG.provinces)
		// for(var i in CONFIG.provinces){
			// if(CONFIG.provinces[i]["area_id"] == poid){
				// area.province = CONFIG.provinces[i]["area_name"];
				// break;
			// }
		// }
		// return area;
	// }
	// JSON转换为字符串 兼容ie6 7
	function _JSONstringify(Json){
		var result = "";
		if(typeof JSON !== "undefined" && typeof JSON.stringify == "function"){
			result = JSON.stringify(Json);   
		}else{
			result = jQuery.parseJSON(Json);
		}
		return result;
	}

	
	
	return { 
		init : init,
		getData : getData,
		buildData : buildData,
		loading : loading
	}
	
})();


var Count = (function(){
	
	//数据缓存
	var oData = {
		/* "33145" : {
			pname : "景区产品名称",
			tickets : {
				"21544" : {
					name : "",
					count : ""
				},
				"26541" : {
					name : "",
					count : ""
				}
			}
		} */
	};
	
	//存储dom元素，有多少存多少提高性能
	var listUl,seledUl,stotalW,total_gprice,total_lprice;
	var keyCode = {};
	function initDom(){ //存放所有初始化dom
		listUl = $("#mlistUl");
		seledUl = $("#seledUl");
		stotalW = $("#stotalW");
		total_gprice = $("#total_gprice");
		total_lprice = $("#total_lprice");
		saveBtn = $("#saveToNext");
	}
	
	//ui层
	var UI = {
		//初始化界面
		init : function(){
			var self = this;
			initDom(); //初始化第一第事就是初始化dom元素
			listUl.on("click",".topUl",function(e){
				var _t = $(e.target);
				if(_t.hasClass("ck") || _t.parent().hasClass("ck")) return;
				var target = $(this);
				var pLi = target.parent();
				var botW = pLi.find(".botW");
				if(botW.is(":animated")) return;
				if(botW.is(":visible")){
					UI.slideUp(pLi);
				}else{
					UI.slideDown(pLi);
				}
			})
			listUl.on("click",".slideUpBtn",function(e){
				var pLi = $(e.target).parents(".itemLi");
				UI.slideUp(pLi);
				return false;
			})
			listUl.on("click",".cbtn",function(e){
				var target = $(e.target);
				var p = target.parent();
				var inp = p.find(".numInp");
				var ov = parseFloat(inp.val());
				if(target.hasClass("addBtn")){
					inp.val(ov+1);
					UI.changeCount(target);
				}else if(ov>0){
					inp.val(ov-1);
					UI.changeCount(target);
				}
				return false;
			})
			listUl.on("keyup",".numInp",function(e){
				var keyCode = e.keyCode;
				var key = e.key;
				var target = $(e.target);
				var ov = parseFloat(target.val());
				//console.log(keyCode);
				if((keyCode == 40) && (ov>0)){ //下
					target.val(ov-1);
					UI.changeCount(target);
				}else if(keyCode == 38){ //上
					target.val(ov+1);
					UI.changeCount(target);
				}else if((keyCode>=96 && keyCode<=105) || (keyCode == 8) || (keyCode>=48 && keyCode<=57)){
					UI.changeCount(target);
				}
			})
			listUl.on("blur",".numInp",function(e){
				var target = $(e.target);
				if(!target.val()){
					target.val(0);
				}
			})
			listUl.on("hover",".level",function(){
			$(this).find(".select_list").show();
			//var selecth=$(".select_list").height();
			//selecth=selecth+48;
			//$(".container_e").css("height",selecth); 
			},function(){
				$(this).find(".select_list").hide();
			});
			listUl.on("click",".select_list li",function(){
				var curTxt = $(this).text();
				$(this).addClass("current").siblings().removeClass("current");
				$(this).parent().siblings(".select_txt").text(curTxt);			
				$(this).parent().hide();
			});
		
			listUl.on("toggle",".click_choose",
			 function(){
				$(this).css("background","#BABCB9");
				$(this).find("div").addClass("on");
				$(this).find(".operate_open").css("color","#BABCB9");
				$(this).find(".operate_close").css("color","#FFFFFF");
				$(this).parent().prevAll().css("color","#D8D8D8")
				$(this).parent().prev(".operate_a").hide();
			 },
			 function(){
				$(this).css("background","#35BA3B");
				$(this).find("div").removeClass("on");
				$(this).parent().prevAll().css("color","#000000")
				$(this).parent().prev(".operate_a").show();
				$(this).parent().prev(".operate_a").css("color","#2380AC")
				$(this).find(".operate_open").css("color","#FFFFFF");
				$(this).find(".operate_close").css("color","#35BA3B");
			 }
		);
			//是否开启转分销
			listUl.on("click",".yujbtn",function(e){
				//var target = $(e.target);
				var list_con = $(this).parents().parents().parents(".list_con");
				var ul_list =$("list_con>span").attr("data-active");
				alert(ul_list)

				
				 var target = $(e.currentTarget);
				var id = target.attr("data-id"); 
				//alert(id)
				var yucln = e.target.className;
						if(yucln == "yujbtn onyuj"){
						$.post('http://www.12301.cc/a_dev.html',{'action':'changeStatus','marketing_status':'2','marketing_id':+id}).done(function(code){
							if(code=='200'){
							//alert(target)
							//alert(111)
								e.target.className = "yujbtn";
							}
						}).error(function(){
							alert('发生错误，无法改变状态');
						});
						
					}else{
						$.post('http://www.12301.cc/a_dev.html',{'action':'changeStatus','marketing_status':'0','marketing_id':+id}).done(function(code){
							if(code=='200'){
							   // document.getElementById('yuj').className = "yujbtn onyuj";
							   //alert(111)
							   target.addClass("onyuj");
							} else {
								//alert(response);
							}
						}).error(function(){
							alert('发生错误，无法改变状态');
						}); 
				}
			})
			
			seledUl.on("click","i.tnum",function(e){
				var target = $(e.target);
				var ov = target.text();
				target.hide();
				var inp = target.next();
				inp.show().focus();
			})
			seledUl.on("blur",".tk input.tnumInp",function(e){
				var target = $(e.target);
				if(target.val().length == 0){
					target.hide().prev().show();
					return;
				}
				var count = parseFloat(target.val());
				if(count<0){
					target.hide().prev().show();
					return;
				}
				var lp = target.parent();
				var ph = lp.parent().find(".tp");
				var pid = ph.find("input.pid").val();
				var pname = ph.find(".pname").text();
				var tid = lp.find("input.tid").val();
				var aid = lp.find("input.aid").val();
				var tname = lp.find(".tn").text();
				doData.change({
					pid : pid,
					pname : pname,
					tid : tid,
					tname : tname,
					count : count
				})
				//console.log(oData)
				UI.reChange();
			})
		},
		changeCount : function(target){
			var pTr = target.parents("tr");
			var pBot = pTr.parents(".botW");
			var pid = pBot.parents(".itemLi").find(".topUl .pid").val();
			var pname = pBot.parents(".itemLi").find(".topUl .tcol_1").text();
			var tid = pTr.find("input.tid").val();
			var aid = pTr.find("input.aid").val();
			var tname = pTr.find(".tname").text();
			var gprice = pTr.find("i.tprice").text();
			var lprice = pTr.find("i.sprice").text();
			var count = pTr.find(".numInp").val();
			doData.change({
				pid : pid,
				pname : pname,
				tid : tid,
				tname : tname,
				count : count,
				gprice : gprice,
				lprice : lprice,
				aid : aid
			})
		},
		slideDown : function(targetLi){
			targetLi.addClass("selected").find(".botW").slideDown();
		},
		slideUp : function(targetLi){
			var s = true;
			targetLi.find(".numInp").each(function(e,inp){
				if($(inp).val() !== "0"){
					s = false;
					return;
				}
			})
			targetLi.find(".botW").slideUp(function(){
				if(!s){ 
					targetLi.addClass("selected")
				}else{
					targetLi.removeClass("selected")
				}
			})
		},
	
		
	};
	var oData = {
		/* "33145" : {
			pname : "景区产品名称",
			tickets : {
				"21544" : {
					name : "",
					count : ""
				},
				"26541" : {
					name : "",
					count : ""
				}
			}
		} */
	};
	//操作oData
	var doData = {
		change : function(json){ 
			var pid = json.pid;
			var lvl = json.lvl;
			var pname = json.pname;
			var tid = json.tid;
			var tname = json.tname;
			var count = json.count;
			var gprice = json.gprice;
			var lprice = json.lprice;
			var aid = json.aid;
			var data = oData[pid];
			var ticket;
			var isEmpty = true;
			//console.log(oData)
			if(count != 0){
				if(data){ //若该产品已被选择
					ticket = data.tickets[tid];
					if(ticket){ //若已有票类存在
						ticket.count = count;
					}else{ //若不存在该票类(需新增该票类)
						data.tickets = data.tickets || {};
						data.tickets[tid] = {};
						data.tickets[tid]["name"] = tname;
						data.tickets[tid]["count"] = count;
						data.tickets[tid]["gprice"] = gprice;
						data.tickets[tid]["lprice"] = lprice;
						data.tickets[tid]["aid"] = aid;
					}	
				}else{ //若该产品未被选择
					oData[pid] = {};
					oData[pid]["pname"] = pname;
					oData[pid]["tickets"] = {};
					oData[pid]["tickets"][tid] = {};
					oData[pid]["tickets"][tid]["name"] = tname;
					oData[pid]["tickets"][tid]["count"] = count;
					oData[pid]["tickets"][tid]["gprice"] = gprice;
					oData[pid]["tickets"][tid]["lprice"] = lprice;
					oData[pid]["tickets"][tid]["aid"] = aid;
				}
			}else{
				if(!data) return;
				data.tickets[tid] = undefined;
				delete data.tickets[tid];
				for(var p in data.tickets){
					isEmpty = false;
				}
				if(isEmpty){
					oData[pid] = undefined;
					delete oData[pid];
				}
			}
			//通知UI更新数据
			$(window).trigger("oData.change");
		}
	};
	
	function getSelected(){ //取得已选择的数据，用于模块间通信
		return oData;
	}
	
	$(window).on("oData.change",UI.change);
	
	return { init : UI.init, getSelected : getSelected}
	
})();


$(function(){
	S.init(function(){
		//List.init();
	});
	List.init();
	Count.init();
})

//重构
;(function(){
	
	var SelectData = {}; //存放已选择的数据
	var oData = {}; //存放ajax一次性抓取的list列表全部数据
	var cData = {}; //数据缓存
	var totalpagesize = 0; //list列表数据总页数
	var oDataSize = 0; //list列表数据总条数
	var sizeperpage = 5; //每页显示几条数据
	var AreaData = {}; //存放省份、市级信息
	var AjaxTimeout = 5 * 1000;
	var Urls = {
		getAllProvs : "http://www.12301.cc/call/getAreas.php",
		getAllListData : "http://www.12301.cc/prod_sale_dev.html"
	}
	/*
	{
		"12" : {
			"name" : "福建省",
			"citys" : {
				{ "001" : "福州" },
				{ "002" : "泉州" }
			}
		},
		"11" : {
		
		}
	}
	****/
	var Obser = (function(){ //事件订阅/发布
		var obser = $({});
		function init(){
			
		}
		function trigger(eventname){
			obser.trigger(eventname)
		}
		return { init : init, trigger : trigger }
	})();
	
	var Ajaxor = (function(){
		
		//取得所有省份
		function getAllProvs(){
			$.ajax({
				url : Urls.getAllProvs,
				type : "GET",
				dataType : "json",
				timeout : AjaxTimeout,
				data : {
					act : "getProvince"
				},
				success : function(res){
					if(res && res.length){
						var prov = {};
						for(var i=0,len=res.length; i<len; i++){
							var provId = res[i]["area_id"];
							AreaData[provId]= {};
							AreaData[provId]["name"] = res[i]["area_name"];
						}
						getAllListData();
					}else{
						alert("获取城市列表出错")
					}
				},
				error : function(xhr,textStatus){
					if(textStatus && textStatus == "timeout"){
						xhr && xhr.abort && typeof xhr.abort == "function" && xhr.abort();
					}
					alert(textStatus)
				}
			})
		}
		//获得所有列表数据
		function getAllListData(){
			//loading();
			$.ajax({
				url : Urls.getAllListData,
				type : "GET",
				dataType : "JSON",
				timeout : AjaxTimeout,
				data : { "sync" : "1" },
				success : function(res){
					if(res && res.total>0){
						oData = res.lists;
						oDataSize = res.total
						//totalpagesize = Math.ceil(res.total/psize); //计算总页数据有小数就整数部分加1
					}else{
						alert("无数据");
					}
				},
				error : function(xhr,textStatus){
					if(textStatus && textStatus == "timeout"){
						if(xhr && xhr.abort && typeof xhr.abort == "function"){
							xhr.abort();
							alert("请求产品列表超时，请稍后重试。");
						}
					}else{
						alert("请求产品列表出错");
					}
				}
			})
		}
		
		function init(){
			getAllProvs();
		}
		
		return {
			init : init 
		}
		
	})();
	
	var Filtor = (function(){
		function filtProv(data,provId){ //过滤出省
			var d = {};
			for(var i in data){
				var _provId = data[i]["areas"][0];
				if(_provId == provId){
					d[i] = data[i];
				}
			}
			return d;
		}
		function filtCity(data,provId,cityId){ //过滤出省市
			var d = filtProv(data,provId);
			var _d = {};
			for(var i in d){
				var _cityId = d[i]["areas"][1];
				if(_cityId == cityId){
					_d[i] = d[i];
				}
			}
			return _d;
		}
		function filtProdname(data,name){ //根据景区产品名称过滤
			var _d = {};
			for(var i in data){
				var title = data[i]["title"];
				if(title.indexOf(name) > -1 ){
					_d[i] = data[i]
				}
			}
			return _d;
		}
		function filtapplyname(data,name){ //根据供应商名称过滤
			var _d = {};
			return _d;
		}
		return {
			filtProv : filtProv,
			filtCity : filtCity,
			filtProdname : filtProdname,
			filtapplyname : filtapplyname
		}
	})();
	//本地数据抓取器
	var Getor = (function(){
		function get(){
			var data = {};
			var args = arguments;
			if(args.length == 0){ //获取全部数据
				data = _get();
			}else if(typeof args[0] == "number" && args[0] !== 0){ //获取指定页数据(用于上一页下一页切换)
				var pageindex = args[0];
				var begin = (pageindex-1) * sizeperpage + 1;
				var end = pageindex * sizeperpage;
				data = _getData(begin,end);
			}else if(typeof args[0] == "object" && typeof args[1] == "number"){ //在指定数据包内抓取数据
				data = _getSubData(args[0],args[1]);
			}
			return data
		}
		function _getSubData(data,pageindex){
			var _d = {};
			var index = 0;
			var begin = (pageindex-1) * sizeperpage + 1;
			var end = pageindex * sizeperpage;
			for(var i in data){
				index++;
				if(index>=begin && index<=end){
					_d[i] = data[i];
				}
			}
			return _d;
		}
		function _get(begin,end){
			var data = {};
			var index = 0;
			if(arguments.length == 0){
				data[i] = oData[i];
				dData[i] = dData[i];
				return data;
			}else if(begin<end){
				for(var i in oData){
					index++;
					if(index>=begin && index<=end){
						data[i] = oData[i];
					}
				}
			}
			return data;
		}
		return {
			get : get
		}
	})();
	
	var buildor = (function(){
		function build(data){
			
		}
	})();
	
	var Counter = (function(){
		
		
	})();
	
	var Matchor = (function(){
		
	})();
	
	
	$(function(){
		//Ajaxor.init();
	})
	
	
	
})();


/* {
	"2778_1433": {
        "p_type": "A", 
        "sapply_did": "1433", 
        "title": "08141622", 
        "py": "0", 
        "lid": "2778", 
        "areas": [
            "12", 
            "381", 
            "0"
        ], 
        "ticket": [
            {}
		]
	},
	"2778_1433": {
        "p_type": "A", 
        "sapply_did": "1433", 
        "title": "08141622", 
        "py": "0", 
        "lid": "2778", 
        "areas": [
            "12", 
            "381", 
            "0"
        ], 
        "ticket": [
            {}
		]
	}
}

{
	total : 450,
	list : []
}  */









